package org.oep.widgets;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;

public class ChoiceWidget {
	private Vector choices = new Vector();
	private int type = TYPE_LIST;
	private int diameter = 0;
	private int minimumDiameter = 0;
	private int direction = DIR_DOWN;
	private int choiceIndex = 0;
	private int x = 0;
	private int y = 0;
	private Font defaultFont = Font.getDefaultFont();
	private Font selectedFont = Font.getDefaultFont();
	private Image cursor;
	
	public static final int DIR_DOWN = 0;
	public static final int DIR_UP = 1;
	
	public static final int TYPE_CIRCULAR = 0;
	public static final int TYPE_LIST = 1;
	
	public ChoiceWidget(int type) {
		this.type = type;
		try {
			cursor = Image.createImage("/res/graphics/cursor.png");
		}
		catch(IOException e) {
			System.err.println(e);
		}
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public int addChoice(Object b) {
		choices.addElement(b);
		
		String s = b.toString();
		int width = defaultFont.charsWidth(s.toCharArray(), 0, s.length()) / 2;
		int height = defaultFont.getHeight() / 2;
		
		minimumDiameter = Math.max(minimumDiameter, 2 * (int)Math.sqrt(width * width + height * height));
		diameter = Math.max(diameter, minimumDiameter);
		
		return choices.size() - 1;
	}
	
	public void paint(Graphics g) {
		Font f = g.getFont();
		g.setFont(defaultFont);
		if(type == TYPE_LIST) {
			paintList(g);
		}
		if(type == TYPE_CIRCULAR)
			paintCircular(g);
		g.setFont(f);
	}
	
	public void paintCircular(Graphics g) {
		if(choices.size() <= 0 || diameter <= 0) return;
		
		int d = diameter - 4;
		
		// Difference in diameter :)
		int dd = Math.abs(diameter - d);
		
		g.setColor(0);
		g.fillArc(x - diameter / 2, y - diameter / 2, diameter, diameter, 0, 360);
		g.setColor(0xFFFFFF);
		g.fillArc(x - diameter / 2 + dd / 2, y - diameter / 2 + dd / 2, d, d, 0, 360);
		
		int stepAngle = 360 / choices.size();
		g.setColor(0xCCCCCC);
		g.fillArc(x - diameter / 2 + dd / 2, y - diameter / 2 + dd / 2, d, d, -choiceIndex * stepAngle, stepAngle);
	
		g.setColor(0);
		String s = choices.elementAt(choiceIndex).toString();
		g.drawString(s, x, y + defaultFont.getHeight() / 2, Graphics.HCENTER | Graphics.BOTTOM);
	}
	
	public void paintList(Graphics g) {
		int mode;
		if(direction == DIR_UP) {
			mode = Graphics.BOTTOM | Graphics.HCENTER;
		} else {
			mode = Graphics.TOP | Graphics.HCENTER;
		}
		
		// Modifier for the y-coordinate
		int p = 0;
		
		// Calculate the widest choice
		int max = 0;
		for(int i = 0; i < choices.size(); i++) {
			String s = choices.elementAt(i).toString();
			max = Math.max(selectedFont.charsWidth(s.toCharArray(), 0, s.length()), max);
		}
		
		g.setColor(0);
		for(int i = 0; i < choices.size(); i++) {
			switch(direction) {
			case DIR_UP: p -= defaultFont.getHeight(); break;
			case DIR_DOWN: p += defaultFont.getHeight(); break;
			}
			
			String s = choices.elementAt(i).toString();
			
			if(i == choiceIndex) {
				g.setFont(selectedFont);
				g.drawImage(cursor, x - max / 2 - 5, y + p + selectedFont.getHeight() / 2, Graphics.RIGHT | Graphics.VCENTER);
			} else {
				g.setFont(defaultFont);
			}
			g.drawString(s, x, y + p, mode);
		}
	}
	
	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public void setChoice(int c) {
		if(c < 0 || c >= choices.size()) return;
		choiceIndex = c;
	}
	
	public void setRadius(int r) {
		diameter = 2 * r;
	}
	
	public void setDiameter(int d) {
		diameter = Math.max(minimumDiameter, d);
	}
	
	public int getRadius() {
		return diameter / 2;
	}
	
	public int getDiameter() {
		return diameter;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public void nextChoice() {
		choiceIndex++;
		
		if(choiceIndex < choices.size()) return;
		
		if(type == TYPE_CIRCULAR) choiceIndex = 0;
		else if(type == TYPE_LIST) choiceIndex = choices.size() - 1;
	}
	
	public void previousChoice() {
		choiceIndex--;
		
		if(choiceIndex >= 0) return;
		
		if(type == TYPE_CIRCULAR) choiceIndex = choices.size() - 1;
		else if(type == TYPE_LIST) choiceIndex = 0;
	}
	
	public int getChoiceIndex() {
		return choiceIndex;
	}
	
	public Object getChoiceObject() {
		return choices.elementAt(choiceIndex);
	}
	
	public void setDefaultFont(Font f) {
		defaultFont = f;
	}
	
	public void setSelectedFont(Font f) {
		selectedFont = f;
	}
}
